//
//  ViewController.m
//  UploadImg
//
//  Created by 郑东喜 on 2016/11/15.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<NSURLConnectionDelegate>


@property (nonatomic,retain) NSURLConnection* aSynConnection;
@property (nonatomic,retain) NSInputStream *inputStreamForFile;
@property (nonatomic,retain) NSString *localFilePath;

//0510523517
//http://yungou.ie1e.com/ifs/upphoto.ashx

@end

@implementation ViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSString *Str = [[[NSString stringWithFormat:@"%s","http://yungou.ie1e.com/ifs/upphoto.ashx"] stringByAppendingString:@"?"] stringByAppendingString:@"token=0510523517"];
    

    
    
    
    //    NSString*str=@"http://192.168.1.163:8080/UploadServer/NewServlet";
    
    //    创建本地服务器的URL
    
    NSString*str=@"http://yungou.ie1e.com/ifs/upphoto.ashx?token=0510523517";
    
    NSURL*url=[NSURL URLWithString:str];
    
    //    创建可变请求
    
    NSMutableURLRequest*request=[NSMutableURLRequest requestWithURL:url];
    
    //    创建请求数据
    
    
    
    UIImage *image = [UIImage imageNamed:@"ico.jpg"];
    NSData *imageData = UIImageJPEGRepresentation(image, 0.1);
    CGFloat length = [imageData length]/1000;
    NSLog(@"PNG image data = %f kb", length);
    
    //    小文件上传
    
    [request setHTTPBody:imageData];
    
    [request setHTTPMethod:@"post"];
    
    //    大文件上传
    
    //    NSInputStream*stream=[[NSInputStream alloc]initWithFileAtPath:[[NSBundle mainBundle] pathForResource:@"" ofType:@""]];
    
    //响应请求
    
    [NSURLConnection connectionWithRequest:request delegate:self];
}


// 收到响应时，会触发
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse{
    NSLog(@"请求成功！");
    NSMutableData *returnInfoData=[[NSMutableData alloc]init];
    
    
    
    NSLog(@"%s  %d  %@",__func__,__LINE__,aResponse);
    
    NSHTTPURLResponse * httpResponse;
    httpResponse = (NSHTTPURLResponse *)aResponse;
    if ((httpResponse.statusCode / 100) != 2) {
        NSLog(@"保存失败");
    } else {
        NSLog(@"保存成功");
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"%s  %d  %@",__func__,__LINE__,@"---");
    
    NSString *data1 = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    NSLog(@"%s  %d  %@",__func__,__LINE__,data1);
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
